import 'package:chewie/chewie.dart' hide MaterialControls;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hi_base/color.dart';
import 'package:hi_base/view_util.dart';
import 'package:net/widget/h_video_controls.dart';
import 'package:orientation/orientation.dart';
import 'package:video_player/video_player.dart';

// 播放器组件
class VideoView extends StatefulWidget {
  final String url;
  final String cover;
  final bool autoPlay;
  final bool looping;
  final double aspectRatio;
  final Widget overlayUI;
  final Widget barrageUI; //弹幕ui

  VideoView({
    Key key,
    @required this.url,
    @required this.cover,
    this.autoPlay = false,
    this.looping = false,
    this.aspectRatio = 16 / 9,
    this.overlayUI,
    this.barrageUI,
  }) : super(key: key);

  @override
  _VideoViewState createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;

  //封面
  get _placeholder => FractionallySizedBox(
        widthFactor: 1,
        child: cachedImage(widget.cover),
      );

  //进度条颜色配置
  get _progressColors => ChewieProgressColors(
        playedColor: primary,
        handleColor: primary,
        backgroundColor: Colors.grey,
        bufferedColor: primary[50],
      );
  @override
  void initState() {
    super.initState();
    // 初始化播放器设置
    _videoPlayerController = VideoPlayerController.network(widget.url);
    // 更多参数见 https://blog.csdn.net/weixin_38025168/article/details/103921915
    _chewieController = ChewieController(
      allowMuting: false, //定义是否应显示静音控件
      videoPlayerController: _videoPlayerController, //视频的控制器
      aspectRatio: widget.aspectRatio, //视频的宽高比
      autoPlay: widget.autoPlay, //自动播放
      looping: widget.looping, //循环播放
      placeholder: _placeholder, //占位
      allowPlaybackSpeedChanging: false, //定义播放器是否全屏睡眠
      customControls: MaterialControls(
        //自定义样式
        showLoadingOnInitialize: false,
        showBigPlayIcon: false,
        bottomGradient: blackLinearGradient(),
        overlayUI: widget.overlayUI,
        // 弹幕
        barrageUI: widget.barrageUI,
      ),
      materialProgressColors: _progressColors,
    );
    //fix iOS无法正常退出全屏播放问题
    _chewieController.addListener(_fullScreenListener);
  }

  @override
  void dispose() {
    _chewieController.removeListener(_fullScreenListener);
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double playerHeight = screenWidth / widget.aspectRatio;

    return Container(
      width: screenWidth,
      height: playerHeight,
      color: Colors.grey,
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  void _fullScreenListener() {
    Size size = MediaQuery.of(context).size;
    if (size.width > size.height) {
      // 强制恢复成竖屏
      OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
    }
  }
}
